package cake;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import user.DBCoon;

public class UserDaoImpl implements UserDao {
    /*创建一个返回值标志*/
    static boolean flag = false;
    /*提高sql作用域，每个方法里都要编写sql语句*/
    static String sql = null;
    public boolean login(String name, String pwd) {
        try {
            DBCoon.init();
            String sql = "select * from user where name= ? and password = ?";
            /*使用预编译SQL传递参数*/
            Object[] args = {name,pwd};
            ResultSet rs = DBCoon.searchSql(sql, args);
            System.out.println("yes");
            /*遍历数据库里的账号密码与从jsp传来的账号密码作对比*/
            while (rs.next()) {
                if (rs.getString("name").equals(name)&&rs.getString("password").equals(pwd)){
                    flag = true;
                    System.out.println("登录成功");
                }
            }
        }catch (Exception e) {
            System.out.println("登录失败");
            e.printStackTrace();
        } finally {
            DBCoon.close();
        }
        return flag;
    }

    public boolean admlogin(String name, String pwd) {
        try {
            DBCoon.init();
            String sql = "select * from admlogin where name = ? and password = ?";
            /*使用预编译SQL传递参数*/
            Object[] args = {name,pwd};
            ResultSet rs = DBCoon.searchSql(sql, args);
            System.out.println("yes");
            /*遍历数据库里的账号密码与从jsp传来的账号密码作对比*/
            while (rs.next()) {
                if (rs.getString("name").equals(name)&&rs.getString("password").equals(pwd)){
                    flag = true;
                    System.out.println("登录成功");
                }
            }
        }catch (Exception e) {
            System.out.println("登录失败");
            e.printStackTrace();
        } finally {
            DBCoon.close();
        }
        return flag;
    }

    public boolean register(User user) {
        try {
            DBCoon.init();
            sql="insert into user(name, password, email) values(?,?,?) ";
            Object[] args = {user.getName(),user.getPwd(),user.getEmail()};
            int i = DBCoon.addUpdDel(sql,args);
            if (i>0){
                flag = true;
                System.out.println("注册成功");
            }
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("注册失败");
        }finally {
            DBCoon.close();
        }
        return flag;
    }

    public List<Cake> searchAll() {
        List<Cake> list = new ArrayList<Cake>();
        try {
            DBCoon.init();
            sql = "select * from goods";
            ResultSet rs = DBCoon.getUsers(sql);
            System.out.println("获取所有用户信息成功");
            while (rs.next()) {
                Cake cake = new Cake();
                cake.setId(rs.getString("id"));
                cake.setShop(rs.getString("shop"));
                cake.setPrice(rs.getString("price"));
                cake.setNum(rs.getString("num"));
                list.add(cake);
            }
        }catch (Exception e){
            System.out.println("获取所有用户信息失败");
            e.printStackTrace();
        }finally{
            DBCoon.close();
        }
        return list;
    }

    public boolean adduser(User user){
        try {
            DBCoon.init();
            sql="insert into user(name,password,email)"+"values(?,?,?)";
            Object[] args = {user.getName(),user.getPwd(),user.getEmail()};
            int i = DBCoon.addUpdDel(sql,args);
            if (i>0){
                flag = true;
                System.out.println("添加成功");
            }
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("添加失败");
        }finally {
            DBCoon.close();
        }
        return flag;
    }


    public boolean addcake(Cake cake){
        try {
            DBCoon.init();
            sql="insert into shop(id, shop, detail, price, num)"+"values(?,?,?,?,?)";
            Object[] args = {cake.getId(), cake.getShop(),cake.getDetail(),cake.getPrice(),cake.getNum()};
            int i = DBCoon.addUpdDel(sql,args);
            if (i>0){
                flag = true;
                System.out.println("商品添加成功");
            }
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("商品添加失败");
        }finally {
            DBCoon.close();
        }
        return flag;
    }

    public boolean update(String id, String shop,String detail, String price, String num) {
        try {
            DBCoon.init();
            sql = "update shop set id=?,shop=?,detail=?,price=?,num=? where id=?";
            Object[] args={id,shop,detail,price,num,id};
            int i = DBCoon.addUpdDel(sql,args);
            if (i>0){
                flag=true;
                System.out.println("商品修改成功");
            }
        }catch (Exception e) {
            System.out.println("商品修改失败");
            e.printStackTrace();
        }finally {
            DBCoon.close();
        }
        return flag;
    }

    public boolean delete(int id) {
        try {
            DBCoon.init();
            sql = "delete from user where id = ?";
            Object[] args = {id};
            int i = DBCoon.addUpdDel(sql,args);
            if (i>0){
                flag=true;
                System.out.println("删除成功");
            }
        }catch (Exception e) {
            System.out.println("删除失败");
            e.printStackTrace();
        }finally {
            DBCoon.close();
        }
        return flag;
    }

}